package com.ywu.application.standard.role.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.result.CommonResult;
import com.ywu.application.standard.role.data.ApiRoleAuthRepository;
import com.ywu.application.standard.role.data.UserRoleRepository;
import com.ywu.application.standard.role.dto.ApiRoleAuthDto;
import com.ywu.application.standard.role.dto.ApiRoleAuthRolePermissionDto;
import com.ywu.application.standard.role.entity.ApiRoleAuthEntity;
import com.ywu.application.standard.role.entity.UserRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName ApiRoleAuthService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/9 22:01
 * @Version 1.0
 **/
@Service
public class ApiRoleAuthService {
    @Autowired
    private ApiRoleAuthRepository apiRoleAuthRepository;

    @Autowired
    private UserRoleRepository userRoleRepository;

    /**
    * @Author GroundDemo
    * @Description 查询API关联的角色
    * @Date 22:02 2024/3/9
    * @Param [apiRoleAuthDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryApiRoleInfo(ApiRoleAuthDto apiRoleAuthDto) {
        List<ApiRoleAuthEntity> apiRoleAuthEntities = apiRoleAuthRepository.queryApiRoleInfo(apiRoleAuthDto);
        if (Objects.isNull(apiRoleAuthEntities)) {
            return CommonResult.success(new ArrayList<>());
        }
        List<Integer> roleIds = apiRoleAuthEntities.stream().map(ApiRoleAuthEntity::getRoleId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(roleIds)) {
            return CommonResult.success(new ArrayList<>());
        }
        List<UserRoleEntity> entities = userRoleRepository.queryBatchByRoleIds(roleIds);
        return CommonResult.success(entities);
    }

    /**
    * @Author GroundDemo
    * @Description API角色授权管理
    * @Date 22:12 2024/3/9
    * @Param [apiRoleAuthDtos]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> apiRoleAuthCreateInfo(ApiRoleAuthRolePermissionDto apiRoleAuthDtos) {
        List<ApiRoleAuthDto> rolePermissionList = apiRoleAuthDtos.getRolePermissionList();
        // 先删除api的全部权限
        apiRoleAuthRepository.deleteBatchByApiId(Collections.singletonList(apiRoleAuthDtos.getApiId()));
        ArrayList<ApiRoleAuthEntity> apiRoleAuthEntities = new ArrayList<>();
        for (ApiRoleAuthDto dto : rolePermissionList) {
            ApiRoleAuthEntity apiRoleAuthEntity = new ApiRoleAuthEntity();
            YwuContext currentContext = YwuContextHolder.getCurrentContext();
            apiRoleAuthEntity.setApiId(apiRoleAuthDtos.getApiId());
            apiRoleAuthEntity.setRoleId(dto.getId());
            apiRoleAuthEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
            apiRoleAuthEntities.add(apiRoleAuthEntity);
        }
        if (CollectionUtils.isEmpty(apiRoleAuthEntities)) {
            return CommonResult.success(new ArrayList<>());
        }
        apiRoleAuthRepository.batchInsertApiRoleInfo(apiRoleAuthEntities);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除api授权信息
    * @Date 19:17 2025/3/26
    * @Param [apiRoleAuthDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> apiRoleAuthDeleteInfo(ApiRoleAuthDto apiRoleAuthDto) {
        apiRoleAuthRepository.deleteBatchByApiId(Collections.singletonList(apiRoleAuthDto.getApiId()));
        return CommonResult.success(null);
    }
}
